package com.briup.estore.service.Impl;

import com.briup.estore.bean.Customer;
import com.briup.estore.bean.CustomerExample;
import com.briup.estore.dao.CustomerMapper;
import com.briup.estore.service.CustomerService;
import com.briup.estore.util.MyBatisSqlSessionFactory;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.List;

public class CustomerServiceImpl implements CustomerService {
    private CustomerMapper customerMapper;
    {
        try {
            SqlSession sqlSession = MyBatisSqlSessionFactory.openSession();
            customerMapper = sqlSession.getMapper(CustomerMapper.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Override
    public Customer login(String username, String password) {
        CustomerExample example = new CustomerExample();
        example.createCriteria().andNameEqualTo(username)
                .andPasswordEqualTo(password);
        List<Customer> customers = customerMapper.selectByExample(example);
        if (customers ==null || customers.size()==0){
            return null;
        }else {
            return customers.get(0);
        }
    }

    @Override
    public void register(Customer customer) {
        customerMapper.insert(customer);
    }

    @Override
    public void logout(Integer customerId) {

    }
}
